package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/10 21:15
 */
public class AC1016最大上升子序列和 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[] nums = new int[n];
        s = reader.readLine().split("\\s+");
        for (int i = 0; i < n; i++) nums[i] = Integer.parseInt(s[i]);
        writer.write(String.valueOf(dynamicProgramming(nums)));
        writer.flush();
    }

    private static int dynamicProgramming(int[] nums) {
        int[] dp = new int[nums.length];
        int answer = 0;
        for (int i = 0; i < nums.length; i++) {
            dp[i] = nums[i];
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) dp[i] = Math.max(dp[i], dp[j] + nums[i]);
            }
            answer = Math.max(answer, dp[i]);
        }
        return answer;
    }
}
